package com.cashmanager.client.view;



import com.cashmanager.client.CashPage;
import com.cashmanager.client.Workspace;
import com.smartgwt.client.types.Alignment;
import com.smartgwt.client.widgets.Canvas;
import com.smartgwt.client.widgets.IButton;
import com.smartgwt.client.widgets.Label;
import com.smartgwt.client.widgets.StretchImgButton;
import com.smartgwt.client.widgets.menu.events.ClickHandler; 
import com.smartgwt.client.widgets.menu.events.MenuItemClickEvent;
import com.smartgwt.client.widgets.events.ClickEvent;
import com.smartgwt.client.widgets.layout.HLayout;
import com.smartgwt.client.widgets.layout.VLayout;
import com.smartgwt.client.widgets.menu.Menu;
import com.smartgwt.client.widgets.menu.MenuButton;
import com.smartgwt.client.widgets.menu.MenuItem;



public class MainPage extends VLayout{
	
	private CashPage cashPage;
	private FalseCashPage falseCashListGrid;
	private WarnCashPage warnCashListGrid;
	private BranchPage branchListGrid;
	private ATMPage atmListGrid;
	private NewAnalysisPage newAnalysisPage;
	private RunAnalysisPage runAnalysisPage;
	private CashierCompanyWindow cashierCompanyWindow;
	private Workspace workspace;
	private HLayout menuLayout;

	public MainPage() {
		workspace = new Workspace();
		workspace.setWidth100();
		workspace.setHeight100();
		workspace.setAlign(Alignment.CENTER);
//		mainLayout.setPadding(5);
		final CashPage cash = new CashPage();
		workspace.addMember(cash);
		setWidth100();
		setHeight100();	
		addMember(makeHeader());
		addMember(makeMenu());
		final Label l = new Label();
		l.setHeight(5);
		addMember(l);
		addMember(workspace);	
		draw();
	}
	
	private HLayout makeMenu() {
		
		menuLayout = new HLayout();
		menuLayout.setAlign(Alignment.LEFT);
		menuLayout.setWidth100();
		menuLayout.setHeight(30);
		
		
		final IButton homeButton = new IButton("点钞机现金查询");
		homeButton.setWidth(150);
		homeButton.setHeight(30);
		homeButton.hideClickMask();
		homeButton.setAlign(Alignment.CENTER);
		homeButton.addClickHandler(new 
				com.smartgwt.client.widgets.events.ClickHandler(){

			public void onClick(ClickEvent event) {
				workspace.hideAllMemebers();
				if (cashPage == null){
					cashPage = new CashPage();
					workspace.addMember(cashPage);
				}
				cashPage.show();
				unSelectAllButtons();
				homeButton.setSelected(true);
			}
			
		});
		Menu trace = new Menu();
		MenuItem falseCash = new MenuItem("假币审核");
		MenuItem warnCash = new MenuItem("可疑币审核 ");
		MenuItem branchCash = new MenuItem("网点追溯");
		MenuItem ATMCash = new MenuItem("ATM机追溯");
		
		Menu atmMenu = new Menu();
		MenuItem atmAllo = new MenuItem("ATM配钞");
		
		atmMenu.setItems(atmAllo);
		final MenuButton btn2 = new MenuButton("信息追溯", trace);
		btn2.setWidth(150);
		btn2.setHeight(30);
		btn2.setAlign(Alignment.CENTER);
		btn2.hideClickMask();		
	
		Menu analysis = new Menu();
		MenuItem newAnalysis = new MenuItem("创建执行统计分析 ");
		MenuItem runAnalysis = new MenuItem("执行已存统计分析 ");
		
		final MenuButton btn3 = new MenuButton("统计分析", analysis);
		btn3.setWidth(150);
		btn3.setHeight(30);
		btn3.setAlign(Alignment.CENTER);
		
		falseCash.addClickHandler(new ClickHandler(){

			public void onClick(MenuItemClickEvent event) {
				workspace.hideAllMemebers();
				if (falseCashListGrid == null){
					falseCashListGrid = new FalseCashPage();
					workspace.addMember(falseCashListGrid);
				}
				falseCashListGrid.show();
				unSelectAllButtons();
				btn2.setSelected(true);
			}
			
		});
		warnCash.addClickHandler(new ClickHandler(){

			public void onClick(MenuItemClickEvent event) {
				workspace.hideAllMemebers();
				if (warnCashListGrid == null){
					warnCashListGrid = new WarnCashPage();
					workspace.addMember(warnCashListGrid);
				}
				warnCashListGrid.show();
				unSelectAllButtons();
				btn2.setSelected(true);
			}
			
		});
		
		branchCash.addClickHandler(new ClickHandler(){

			public void onClick(MenuItemClickEvent event) {
				workspace.hideAllMemebers();
				if (branchListGrid == null){
					branchListGrid = new BranchPage();
					workspace.addMember(branchListGrid);
				}
				branchListGrid.show();
				unSelectAllButtons();
				btn2.setSelected(true);
			}
			
		});
		ATMCash.addClickHandler(new ClickHandler(){

			public void onClick(MenuItemClickEvent event) {
				workspace.hideAllMemebers();
				if (atmListGrid == null){
					atmListGrid = new ATMPage();
					workspace.addMember(atmListGrid);
				}
				atmListGrid.show();
				unSelectAllButtons();
				btn2.setSelected(true);
			}
			
		});
		trace.setItems(falseCash, warnCash, branchCash );
		
	
		newAnalysis.addClickHandler(new ClickHandler(){

			public void onClick(MenuItemClickEvent event) {
				workspace.hideAllMemebers();
				if (newAnalysisPage == null){
					newAnalysisPage = new NewAnalysisPage();
					workspace.addMember(newAnalysisPage);
				}
				newAnalysisPage.show();
				unSelectAllButtons();
				btn3.setSelected(true);
			}
			
		});
		runAnalysis.addClickHandler(new ClickHandler(){

			public void onClick(MenuItemClickEvent event) {
				workspace.hideAllMemebers();
				if (runAnalysisPage == null){
					runAnalysisPage = new RunAnalysisPage();
					workspace.addMember(runAnalysisPage);
				}
				runAnalysisPage.show();
				unSelectAllButtons();
				btn3.setSelected(true);
			}
			
		});
		
		analysis.setItems(newAnalysis, runAnalysis);
	
		menuLayout.addMember(homeButton);		
		menuLayout.addMember(btn2);
		menuLayout.addMember(btn3);
//		menuLayout.setPadding(5);
		Menu adminMenu = new Menu();
		MenuItem organManagement = new MenuItem("机构管理");
		MenuItem userManagement = new MenuItem("用户管理");
		MenuItem atmManagement = new MenuItem("ATM机管理");
		MenuItem cashierManagement = new MenuItem("点钞机管理");
		
		Menu organSubMenu = new Menu();
		MenuItem addOrgan = new MenuItem("添加新机构");
		MenuItem mainOrgan = new MenuItem("现有机构维护");
		organSubMenu.setItems(addOrgan, mainOrgan);
		organManagement.setSubmenu(organSubMenu);
		
		Menu userSubMenu = new Menu();
		MenuItem addUser = new MenuItem("添加新用户");
		MenuItem mainUser = new MenuItem("现有用户维护");
		userSubMenu.setItems(addUser, mainUser);
		userManagement.setSubmenu(userSubMenu);
		
		Menu atmSubMenu = new Menu();
		MenuItem addAtm = new MenuItem("添加新ATM");
		MenuItem mainAtm = new MenuItem("现有ATM维护");
		atmSubMenu.setItems(addAtm, mainAtm);
		atmManagement.setSubmenu(atmSubMenu);
		
		Menu cashierSubMenu = new Menu();
		MenuItem addCashier = new MenuItem("点钞机厂家维护");
		MenuItem mainCashier = new MenuItem("机构维护");
		cashierSubMenu.setItems(addCashier, mainCashier);
		cashierManagement.setSubmenu(cashierSubMenu);
		
		addCashier.addClickHandler(new ClickHandler(){

			public void onClick(MenuItemClickEvent event) {
//				workspace.hideAllMemebers();
				if (cashierCompanyWindow == null){
					cashierCompanyWindow = new CashierCompanyWindow();
//					workspace.addMember(cashierCompanyWindow);
				}
				cashierCompanyWindow.show();
//				unSelectAllButtons();
				btn3.setSelected(true);
			}
			
		});		
		adminMenu.setItems(organManagement, userManagement, 
				atmManagement, cashierManagement);
		final MenuButton adminButton = new MenuButton("基础管理",adminMenu);
		adminButton.setWidth(150);
		adminButton.setHeight(30);
		menuLayout.addMember(adminButton);
		IButton emptyButton = new IButton(" ");
		emptyButton.setWidth100();
		emptyButton.setHeight(30);
		menuLayout.addMember(emptyButton);
		
		//menuLayout.setBorder("1px solid gray");
		return menuLayout;
		
	}

	private HLayout makeHeader() {
		HLayout headerLayout = new HLayout();
		headerLayout.setWidth100();
		headerLayout.setHeight(30);
//		headerLayout.setBackgroundImage("./cashPortalHeaderBackground.jpg");
		headerLayout.setBackgroundColor("#73B7FF");
//		headerLayout.setPadding(5);
//		DynamicForm loginForm = new DynamicForm();
		Label dsLabel = new Label();
//		dsLabel.setContents("Test");
		dsLabel.setContents("<font color= \"white\">"
				+"&nbsp;&nbsp;登陆用户: 测试用户 </font>");
////		dsLabel.setWidth("100%");
		dsLabel.setWrap(false);
//		loginForm.setWidth("30%");
//		loginForm.setNumCols(3);
//		loginForm.setFields(dsLabel);
		headerLayout.addMember(dsLabel);
			
		return headerLayout;
	}
	
	public void unSelectAllButtons() {
		Canvas canves[] = menuLayout.getMembers();
		for(Canvas canve: canves) {
			if(canve instanceof StretchImgButton) {
				StretchImgButton btn = (StretchImgButton)canve;
				btn.setSelected(false);
				btn.setCanFocus(false);
			}else if (canve instanceof MenuButton){
				MenuButton btn = (MenuButton)canve;
				btn.setSelected(false);
				btn.setCanFocus(false);
			}
		}
	}
}
